using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace SinceTimes.NetWork
{
    public class TextMSG:IPeerMessage
    {

        public uint Length { get;internal set; }
        public uint Serial { get; internal set; }

        public int MessageIndex { get; internal set; }
        public ushort MessageType { get; internal set; }

        public string Text;
        public Encoding _Encoding=  Encoding.UTF8;

        bool IPeerMessage.Empty =>string.IsNullOrEmpty(Text);

        public TextMSG(byte[] _buffer, uint _offset, uint _size)
        {
            Decode(_buffer, _offset, _size);
        }
        public TextMSG()
        {
            Text = "";
        }
        public TextMSG(string _text)
        {
            Text = _text;
        }
        public byte[] Encode()
        {
            return _Encoding.GetBytes(Text);
        }
        
        public void Decode(byte[] _buffer, uint _offset, uint _size)
        {
            Length = (uint)_size;

            Text = _Encoding.GetString(_buffer, (int)_offset, (int)_size);

            _offset = _size - 1;
        }



        public override string ToString()
        {
            return Text;
        }
        public T DataAs<T>()
        {
            return default(T);// (T)_Buff;
        }

    }


    public class TextPeer : Peer<TextMSG>
    {
        public TextPeer() { }
        protected override TextMSG _DoDecode(byte[] _buffer, uint _offset, uint _size)
        {
            TextMSG msg= _size>0?  new TextMSG(_buffer, _offset, _size):null;

            return msg;
        }



    }

}
